<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_SESSION['role_id']>6 && $_SESSION['role_id']<15){
	exi('您无权访问！','error');
}
//客户列表
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'shmen_time >'=>100);
	if($_SESSION['role_id']==5 || $_SESSION['role_id']==6){
		$params['worker'.$_SESSION['role_id'].'id']=$_SESSION['workerid'];
	}
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['worker5id']){
		$params['worker5id']=intval($_RQ['worker5id']);
		$return['params']['worker5id']=$_RQ['worker5id'];
	}
	if($_RQ['worker6id']){
		$params['worker6id']=intval($_RQ['worker6id']);
		$return['params']['worker6id']=$_RQ['worker6id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['shmen_time between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==1){
				$item['from_typetitle']=$this->member_from1_type[$item['from1_type']];
			}elseif($item['from_type']==2){
				$item['from_typetitle']=$this->member_from2_type[$item['from2_type']];
			}else{
				$item['from_typetitle']=$this->member_from_type[$item['from_type']];
			}
			if($item['shmen_time']>0){
				$this->model('uplog')->upworkerlog($item,4,true);
				$this->model('uplog')->upworkerlog($item,32,true);
			}
			if($item['status']==-1){
				$this->model('uplog')->upworkerlogstatus($item['id'],-1);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'edit'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio2','options'=>'jibie_type'),
			array('name'=>'from_typetitle','title'=>'渠道','type'=>'string'),
			array('title'=>'网络员','name'=>'worker3id','type'=>'select2','options'=>'workers'),
			array('title'=>'前台专员','name'=>'worker4id','type'=>'select2','options'=>'workers'),
			array('title'=>'电客员','name'=>'worker5id','type'=>'select2','options'=>'workers'),
			array('title'=>'销售员','name'=>'worker6id','type'=>'select2','options'=>'workers'),
			array('title'=>'体验员','name'=>'worker7id','type'=>'select2','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio2','options'=>'status'),
			array('name'=>'shmen_time','title'=>'意向日期','type'=>'time'),
			array('type'=>'href','action'=>'/member.genjin_log/onelist/','title'=>'查看跟进记录'),
		),'action'=>array(
			array('type'=>'modal','op'=>'look','title'=>'客户详情'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'10','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'10','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'status','width'=>'10','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','width'=>15,'title'=>'日期','type'=>'time_range'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'type'=>array(0,2)),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['workers6']=$this->getall('worker',array('role'=>6,'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['workers5']=$this->getall('worker',array('role'=>5,'uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'),'id');
	$return['status']=$this->member_status;
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='shmen'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'type'=>32);
	if($_SESSION['role_id']==5 || $_SESSION['role_id']==6){
		$params['workerid']=$_SESSION['workerid'];
	}
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($_RQ['workerid']){
		$params['workerid']=intval($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['title'])){
		$params2=array('uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$memberids=$this->getall('member',$params2,array('id'),'id');
		$memberids=$memberids?array_keys($memberids):-1;
		$params['memberid']=$memberids;
		$return['params']['title']=$_RQ['title'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	if($_RQ['psize']){
		$return['params']['psize']=$_RQ['psize'];
	}
	$list=pdo_getslice('jy_yxw_workerlog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['memberid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'edit'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'storeid','title'=>'所属门店','type'=>'select2','options'=>'stores'),
			array('title'=>'员工','name'=>'workerid','type'=>'select2','options'=>'workers'),
			array('name'=>'createtime','title'=>'意向日期','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>'10','title'=>'客户名称','name'=>'title','help'=>''),
			array('name'=>'createtime','width'=>15,'title'=>'日期','type'=>'time_range'),
		),
	);
	$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid'],'type'=>array(0,2)),array('id','title'),'id');
	$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='look'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($item['birth']){
			$item['age']=date('Y')-$item['birth'];
		}
		$fee2=pdo_get('jy_yxw_infee_log',array('memberid'=>$id,'uniacid'=>$_SESSION['uniacid'],'type'=>2,'status'=>1));
		if($fee2){
			$item['isfee2']=1;
		}
		$item['fee2']=$fee2['fee'];
		$item['fee2day']=$fee2['jiao_day'];
		$fee2=pdo_get('jy_yxw_tuifee_log',array('memberid'=>$id,'uniacid'=>$_SESSION['uniacid'],'type'=>2,'status'=>4));
		if($fee2){
			$item['istuifee2']=1;
		}
		$item['tuifee2']=$fee2['tuifee'];
		$item['tuifee2day']=$fee2['tuiday'];
		$fee=pdo_get('jy_yxw_infee_log',array('memberid'=>$id,'uniacid'=>$_SESSION['uniacid'],'type'=>1,'status'=>1));
		if($fee){
			$item['isfee']=1;
		}
		$item['fee']=$fee['fee'];
		$item['feeday']=$fee['jiao_day'];
		$item['fen_log']=$fee['fen_log'];
		$item['pay_type']=$fee['pay_type'];
		$fee=pdo_get('jy_yxw_tuifee_log',array('memberid'=>$id,'uniacid'=>$_SESSION['uniacid'],'type'=>array(0,1),'status'=>4));
		if($fee){
			$item['istuifee']=1;
		}
		$item['tuifee']=$fee['tuifee'];
		$item['tuifeeday']=$fee['tuiday'];
		if($item['fen_log']){
			foreach($item['fen_log'] as &$fv){
				$fv['timenum']=strtotime($fv['time']);
			}
			paixu($item['fen_log'],'timenum','asc');
			foreach($item['fen_log'] as $k=>&$nfv){
				$sn=$k+1;
				$nfv['sn']='第'.$sn.'期';
			}
		}
	}else{
		//exi('请选择客户！','error');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'look_tab'=>true,
		'looks'=>array(
			array(
				'title'=>'个人信息',
				'list'=>array(
					array('type'=>'string','title'=>'客户名称','name'=>'title'),
					array('type'=>'radio','title'=>'性别','name'=>'sex','options'=>'sex'),
					array('type'=>'string','title'=>'年龄','name'=>'age','help'=>''),
					array('type'=>'string','title'=>'手机号','name'=>'mobile'),
					array('type'=>'string','title'=>'备用手机号','name'=>'mobile2'),
					array('name'=>'from_type','title'=>'渠道','type'=>'radio','options'=>'from_type'),
					array('name'=>'from1_type','title'=>'实体来源渠道','type'=>'radio','options'=>'member_from1_type','show'=>array('from_type'=>1)),
					array('name'=>'from2_type','title'=>'网络来源渠道','type'=>'radio','options'=>'member_from2_type','show'=>array('from_type'=>2)),
					array('type'=>'string','title'=>'微信号','name'=>'wechat'),
					array('type'=>'string','title'=>'邮箱','name'=>'email'),
					array('type'=>'string','title'=>'QQ','name'=>'qq'),
					array('type'=>'string','title'=>'专业','name'=>'zhuanye'),
					array('type'=>'string','title'=>'毕业学校','name'=>'school'),
					array('type'=>'radio','title'=>'学历','name'=>'xueli','options'=>'xueli_type'),
					array('type'=>'string','title'=>'应聘岗位','name'=>'gangwei'),
					array('type'=>'string','title'=>'工作经验','name'=>'workdesc'),
					array('type'=>'pcd','title'=>'户籍','name'=>'pcd','help'=>''),
					array('type'=>'image','title'=>'学信网/证书','name'=>'xueliimg','help'=>''),
				)
			),
		   array(
				'title'=>'缴费信息',
				'list'=>array(
					array('type'=>'string','title'=>'交定金时间','name'=>'fee2day','show'=>array('isfee2'=>1)),
					array('type'=>'string','title'=>'定金金额','name'=>'fee2','show'=>array('isfee2'=>1)),
					array('type'=>'string','title'=>'成交时间','name'=>'feeday','show'=>array('isfee'=>1)),
					array('type'=>'string','title'=>'单费金额','name'=>'fee','show'=>array('isfee'=>1)),
					array('type'=>'string','title'=>'退交定金时间','name'=>'tuifee2day','show'=>array('istuifee2'=>1)),
					array('type'=>'string','title'=>'退定金金额','name'=>'tuifee2','show'=>array('istuifee2'=>1)),
					array('type'=>'string','title'=>'退成交时间','name'=>'tuifeeday','show'=>array('istuifee'=>1)),
					array('type'=>'string','title'=>'退单费金额','name'=>'tuifee','show'=>array('istuifee'=>1)),
					array('type'=>'array','title'=>'分期明细','name'=>'fen_log','show'=>array('pay_type'=>1),
						'list'=>array(
								array('type'=>'string','name'=>'sn','title'=>'期号'),
								array('type'=>'unitstring','unit'=>'元','name'=>'fee','title'=>'当期金额'),
								array('type'=>'string','name'=>'time','title'=>'当期时间'),
						),
					),
					array('type'=>'string','title'=>'身份证号码','name'=>'cardno','help'=>''),
					array('type'=>'string','title'=>'详细地址','name'=>'address','help'=>''),
					array('type'=>'string','title'=>'银行卡号','name'=>'bankno','help'=>''),
					array('type'=>'string','title'=>'银行支行','name'=>'banktitle','help'=>''),
					array('type'=>'image','title'=>'身份证正面','name'=>'cardimg1','help'=>''),
					array('type'=>'image','title'=>'身份证背面','name'=>'cardimg2','help'=>''),
					array('type'=>'image','title'=>'银行卡正面照片','name'=>'bankimg','help'=>''),
				)
			),
		)
	);
	$return['sex']=$this->sex;
	$return['xueli_type']=$this->xueli_type;
	$return['from_type']=$this->member_from_type;
	$return['member_from1_type']=$this->member_from1_type;
	$return['member_from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}